//-----------------------------------------------------------------------
// <copyright file="ShippingLineDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class ShippingLineDAOBase
    {
        #region Common methods
        public virtual ShippingLine CreateShippingLineFromReader(IDataReader reader)
        {
            ShippingLine item = new ShippingLine();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) item.ID = (int)reader["ID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Abbr"))) item.Abbr = (string)reader["Abbr"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("URL"))) item.URL = (string)reader["URL"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateShippingLineFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateShippingLine methods
            
        public virtual void CreateShippingLine(ShippingLine shippingLine)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShippingLineInsert");
                
                dbCommandWrapper.AddInParameter("@Abbr", DbType.AnsiString, shippingLine.Abbr);
                dbCommandWrapper.AddInParameter("@Name", DbType.String, shippingLine.Name);
                dbCommandWrapper.AddInParameter("@URL", DbType.String, shippingLine.URL);
                dbCommandWrapper.AddOutParameter("@ID", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
                shippingLine.ID = (int)dbCommandWrapper.GetParameterValue("@ID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateShippingLineException, ex);
            }
        }

        #endregion

        #region UpdateShippingLine methods
        
        public virtual void UpdateShippingLine(ShippingLine shippingLine)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShippingLineUpdate");            
                
                dbCommandWrapper.AddInParameter("@ID", DbType.Int32, shippingLine.ID);
                dbCommandWrapper.AddInParameter("@Abbr", DbType.AnsiString, shippingLine.Abbr);
                dbCommandWrapper.AddInParameter("@Name", DbType.String, shippingLine.Name);
                dbCommandWrapper.AddInParameter("@URL", DbType.String, shippingLine.URL);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateShippingLineException, ex);
            }
        }
        
        #endregion

        #region DeleteShippingLine methods
        public virtual void DeleteShippingLine(int id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShippingLineDelete");
                
                dbCommandWrapper.AddInParameter("@ID", DbType.Int32, id);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteShippingLineException, ex);
            }
        }

        #endregion

        #region GetShippingLine methods
        
        public virtual ShippingLine GetShippingLine(int id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShippingLineGet");
                
                dbCommandWrapper.AddInParameter("@ID", DbType.Int32, id);
                
                ShippingLine shippingLine =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        shippingLine = CreateShippingLineFromReader(reader);
                        reader.Close();
                    }
                }
                return shippingLine;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetShippingLineException, ex);
            }
        }
        
        #endregion

        #region GetShippingLines methods
        public virtual ShippingLineCollection GetShippingLines(ShippingLineColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShippingLineGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                ShippingLineCollection shippingLineCollection = new ShippingLineCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        ShippingLine shippingLine = CreateShippingLineFromReader(reader);
                        shippingLineCollection.Add(shippingLine);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return shippingLineCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetShippingLinesException, ex);
            }
        }
        
        public virtual ShippingLineCollection GetShippingLines(ShippingLineColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetShippingLines(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}